package com.situ.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.web.dao.IStudentDao;
import com.situ.web.dao.impl.StudentDaoMySqlImpl;
import com.situ.web.entity.Banji;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.service.IStudentService;

public class StudentServiceImpl implements IStudentService{

	private IStudentDao studentDao = new StudentDaoMySqlImpl();
	
	@Override
	public List<Student> selectAll() {
		List<Student> list = studentDao.selectAll();
//		for (Student student : list) {
//			String name = student.getName();
//			student.setName("Java1907-" + name);
//		}
		return list;
	}

	@Override
	public List<Student> search(String name) {
		return studentDao.search(name);
	}

	@Override
	public boolean insert(Student student) {
		// 1、数据库中查看是不是有该姓名的学生 selectByName(name)
				// 2.1、如果有：表示该名字用户已经存在,不去调用insert
				// 2.2、如果没有：表示该名字用户不存在,去调用insert插入这个用户
				List<Student> list =  studentDao.selectByName(student.getName());
				if (list.size() > 0) {
					System.out.println("该用户已经存在");
					// 该名字用户已经存在
					return true;
				} else {
					int count = studentDao.insert(student);
					return count == 1 ? true : false;
				}
	}

	@Override
	public boolean update(Student student) {
		int count = studentDao.update(student);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}

	@Override
	public List<Student> selectById(Integer id) {
		List<Student> list = studentDao.selectById(id);
		return list;
	}

	@Override
	public Student selectById2(Integer id) {
		
		return  studentDao.selectById2(id);
	}

	@Override
	public PageBean pageList(Integer pageNo, Integer pageSize) {
		PageBean pageBean = new PageBean();
		// 当前是第几页
		pageBean.setPageNo(pageNo);
		// 一页有多少条数据
		pageBean.setPageSize(pageSize);
		// 一共多少数据
		int count = studentDao.getTotalCount();
		// 一共有多少页
		int totalPage = (int) Math.ceil ((double)count/pageSize);
		pageBean.setTotalPage(totalPage);
		// 当前页数据
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentDao.pageList(offset, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

	@Override
	public boolean checkName(String name) {
		ArrayList<Student> list = (ArrayList<Student>) studentDao.selectByName(name);
		return list.size() > 0?true:false;
	}

	@Override
	public ArrayList<Banji> selectBanjiAll() {
		ArrayList<Banji> list = studentDao.selectBanjiAll();
		return list;
	}

	@Override
	public List<Map<String, Object>> selectStudentBanjiInfo() {
		return studentDao.selectStudentBanjiInfo();
	}

}
